package com.hnkywl.sts.service.organ;

import com.hnkywl.sts.dao.organ.GysDao;
import com.hnkywl.sts.dao.organ.LjGysDao;
import com.hnkywl.sts.dao.organ.LjLsDao;
import com.hnkywl.sts.entity.organ.Gys;
import com.hnkywl.sts.entity.organ.LjGys;
import com.hnkywl.sts.entity.organ.LjLs;
import com.hnkywl.sts.util.BaseUtils;
import com.hnkywl.sts.util.ReflectionUtils;
import net.ruixin.orm.hibernate.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: lenovo
 * Date: 13-3-21
 * Time: 下午5:13
 * To change this template use File | Settings | File Templates.
 */
@Service
public class GysService {
    private GysDao gysDao;
    private LjLsDao ljLsDao;
    private LjGysDao ljGysDao;

    @Transactional
    public String findAllLjLs(Long jyjgId,Integer lx,int pageNo, int pageSize) {
        Page<LjLs> ljLsPage = this.ljLsDao.findLjLsList(jyjgId,lx,pageNo, pageSize);
        List<LjLs> ljLsList = ljLsPage.getResult() == null ? new ArrayList<LjLs>() : ljLsPage.getResult();
        Map<String, Object> otherParam = new HashMap<String, Object>();
        otherParam.put("list_ljGysList", "gys.jc");
        String ljLsJson = BaseUtils.fetchCollectionToJson(ljLsList, otherParam, "id", "lj.id", "lj.mc","lj.jyjg.id","lj.jyjg.jc", "zt","ljGysList","processInstanceId");
        return "{\"total\":" + ljLsPage.getTotalCount() + ",\"rows\":[" + ljLsJson + "]}";
    }

    /**
     * 查询供应商明细
     *
     * @param id 名称
     * @return
     */
    @Transactional
    public Gys findGysDetail(Long id) {
        return this.gysDao.get(id);
    }

    /**
     * 查找LjLs
     * @param id
     * @return
     */
    @Transactional
    public LjLs findLjLsDetail(Long id) {
        return this.ljLsDao.get(id);
    }

    @Transactional
    public LjLs findLjDetailByProcessInstance(String processInstanceId) {
        return this.ljLsDao.findUniqueByProperty("processInstanceId", processInstanceId);
    }

    /**
     * 删除修改供应商记录
     * @param id
     */
    @Transactional
    public void delete(Long id) {
        LjLs ljLs = this.ljLsDao.get(id);
        ljLs.setDeleted(1);
        ljLs.setLastUpdated(new Date());
        this.ljLsDao.save(ljLs);
        for(LjGys ljGys : ljLs.getLjGysList()){
            ljGys.setDeleted(1);
            ljGys.setLastUpdated(new Date());
            ljGysDao.save(ljGys);
        }
    }

    /**
     * 保存供应商信息
     *
     * @param
     * @return
     */
    @Transactional
    public void saveGys(Gys gys) {
        gys.setSfqy(true);
        gys.setDateCreated(new Date());
        gys.setLastUpdated(gys.getLastUpdated());
        this.gysDao.save(gys);
    }

    /**
     * 改变修改供应商状态
     * @param ljLs
     */
    @Transactional
    public void changeState(LjLs ljLs) {
        ljLs.setLastUpdated(new Date());
        this.ljLsDao.save(ljLs);
    }

    /**
     * 删除供应商信息
     *
     * @param id 岗位ID
     * @return
     */
    @Transactional
    public void deleteGys(Long id) {
        Gys gys = gysDao.get(id);
        gys.setDeleted(1);
        gys.setLastUpdated(new Date());
        gys.setDeleted(1);
        this.gysDao.save(gys);
    }

    /**
     * 更新供应商
     *
     * @param
     * @return
     */
    @Transactional
    public void updateGys(Gys gys) throws RuntimeException {
        Gys clone = gysDao.get(gys.getId());
        clone = (Gys) ReflectionUtils.getInstance(clone, gys);
        clone.setLastUpdated(new Date());
        this.gysDao.save(clone);
    }

    @Autowired
    public void setGysDao(GysDao gysDao) {
        this.gysDao = gysDao;
    }

    @Autowired
    public void setLjLsDao(LjLsDao ljLsDao) {
        this.ljLsDao = ljLsDao;
    }
    @Autowired
    public void setLjGysDao(LjGysDao ljGysDao) {
        this.ljGysDao = ljGysDao;
    }
}
